import wepy from 'wepy';
import api from "@/api/api";
import tip from '@/utils/tip';

export default class orderPayMixin extends wepy.mixin {
    //支付
    async bindToPayMixin(order_no){
        if(!order_no) return;
        const params = {
            login_token:wepy.getStorageSync('login_token'),
            order_no,
            pay_source:1,
            pay_type:3,
            openid:wepy.getStorageSync('userInfo').openid
        }

        let res = await api.setOrderPay(params);
        if (res.data.code === 0) {
            let data = res.data.data.wechat_data;
            data.timeStamp = data.timeStamp.toString();
            return new Promise((resolve)=>{
                return wx.requestPayment({
                    ...data,
                    success: function (res) {
                        tip.success('支付成功')
                        return resolve(true)
                    },
                    fail: function (res) {
                        tip.toast('支付失败',false,'none')
                        return resolve(false)
                    }
                }) 
            })          
        }else{
            tip.toast(res.data.msg,false,'none')
            return false;
        }
    }
    //取消订单or确认收货
    async bindCancelOrderMixin(order_no,type){
        if(!order_no) return;
        type==1 && await tip.confirm('确认取消订单？');
        const params = {
            login_token:wepy.getStorageSync('login_token'),
            order_no,type
        }

        let res = await api.cancelMyOrder(params);
        if (res.data.code === 0) {
            tip.success(type==1?'取消成功':'确认收货成功')
        }else{
            tip.toast(res.data.msg)
        }
    }
    // 验证限时购商品有效性
    async testFlashSaleStock(goods_list){
        const params = {
            login_token:wepy.getStorageSync('login_token'),
            goods_list,
        }
        let res = await api.testFlashSaleStock(params);
        if (res.data.code === 0) {
            return Promise.resolve(res)
        } else {
            return Promise.reject(res.data.msg);
        }
    }
}